﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace Aidbrella.Web.Common
{
    public class Identity : IIdentity
    {
        readonly bool isAuthenticated;
        readonly string name;

        public Identity(bool isAuthenticated, string name)
        {
            this.isAuthenticated = isAuthenticated;
            this.name = name;
        }

        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return isAuthenticated; }
        }

        public string Name
        {
            get { return name; }
        }

        #endregion
    }


    public class LoginUser : IPrincipal
    {
        public string UserName { get; private set; }
        public string Password { get; private set; }
        private List<LoginUserGroup> UserGroups { get; set; }
        public Guid UserId { get; private set; }

        public LoginUser(string userName, string password, List<LoginUserGroup> userGroups, Guid userId)
        {
            UserName = userName;
            Password = password;
            UserGroups = userGroups;
            UserId = userId;
        }

        public IIdentity Identity
        {
            get
            {
                bool isAuthenticated = true;
                return new Identity(isAuthenticated, this.UserName);
            }
        }

        public bool IsInRole(string groupName)
        {
            return UserGroups.Any(group => group.Name.ToUpper() == groupName.ToUpper());
        }

        public bool IsInRole(Guid groupId)
        {
            return UserGroups.Any(group => group.Id == groupId);
        }
    }
    public class LoginUserGroup
    {
        public string Name { get; set; }
        public Guid Id { get; set; }
    }
}